Skip to content

[DowngradePhp81] Add AddReturnTypeWillChangeAttributeRector#372

Open
jquiaios wants to merge 1 commit intorectorphp:mainfrom
jquiaios:add-return-type-will-change-attribute-rector
Open

[DowngradePhp81] Add AddReturnTypeWillChangeAttributeRector#372
jquiaios wants to merge 1 commit intorectorphp:mainfrom
jquiaios:add-return-type-will-change-attribute-rector

Conversation

@jquiaios
Copy link
Copy Markdown

@jquiaios jquiaios commented May 8, 2026

Hello there 👋

Here's a proposition to fix this old issue rectorphp/rector#9515

Here's a global explanation of the issue and proposed fix (if necessary):

PHP 8.1 added provisional return types to built-in interfaces (ArrayAccess, Countable, Iterator, IteratorAggregate and Stringable), triggering deprecation notices on implementations without matching return types.

I created a new AddReturnTypeWillChangeAttributeRector rule that automatically detects affected methods, and adds #[\ReturnTypeWillChange] to suppress those notices.

The rule is registered in the downgrade-php81 set so it runs automatically for anyone using ->withDowngradeSets(php81: true).

The rule is covered by unit tests, including fixtures for the positive cases (ArrayAccess, Countable, Iterator, IteratorAggregate and Stringable), and skip cases (method already has a return type, attribute already present).

- PHP 8.1 added provisional return types to built-in interfaces (ArrayAccess, Countable, Iterator, IteratorAggregate, Stringable), triggering deprecation notices on implementations without matching return types.
- This rule automatically adds #[\ReturnTypeWillChange] to affected methods, suppressing those notices for codebases that need to remain compatible with older PHP versions and cannot yet add the required return types.

Fixes: rectorphp/rector#9515
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant